package com.example.main;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.util.Arrays;

import static org.apache.commons.codec.binary.Base64.encodeBase64String;

public class Main04_Common_ASE {

    public static String keyStr = "34412002dde5a70fc376f378840e9145526e07aafb8592b917819b06db050a41";

//    static {
//        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
//    }

    public static void main(String[] args)  {

        try {
            String pass="pQWrpT2DpA65VwY90pnunBNqi9F34Y2ujZc+/Wwo607Q+zo9tLcnez/LY4KlGvW/4w52kCZV0HI2GmNLGhkGj9J+B1fvlJ5bpr9QmgluroxnE6zPnHSHkN+lSJvkDazoTb/OMz/huwUnNFhqDcikz0KUqBxWSOy2f8huOSnfa37pYMK6wNJXnxBS55h4R9ukYSr4qkv14cC5Z741OqeWiz8BNE2+w9mLXTbqOi+aPNFJwmOBaxuWocjxGvXZDyD3Bz70AOQ1C4WvY3NifO1quSn4CKGRPE1CZz4SNSEsNwoeAmPv5HJB1OZik7SxIvbWecJhszslUk5SG4ziPtJaSxCVKi91S4yqPlctP8D/NfzMY2Wd8WRMJKekWkw+Y1B/CqLxxW0EnSPuWqxwJeI52BZGmG46HI5ptLSnGuW3i2PcwVZjn7EZeASIzor0DohE6Q6w+S+I5S97Jh0v6eEtDV/tpMgA91QADpP4wWzPuXyvh3M/gF80WnEAHTMbdnjEkMku8Ga8c4u4qDDotaAJxlEITb5JbwslFnm1TF8zWgmS2Ttje0LlhBh1GF//oFkVVE2PyHHopOyv8lalZKBPIAN7i31pxrobZY9Z/UqyvYE/1sqU2HqYL8SJbo/YmF12JHyy/kkbZyr15pVIbGDHPMDQzmsw1lSs9K43exTBo/wAAPl7wdcaaVDLOJRs4jaqav6TpgRADQCNCsLEjIFujwCJrOJFeDKJRXgpGz/PBi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FJZmOK4YGkcqO35oNJdp9kRHc4QxQU6SRbZWqc7WSP";
            System.out.println(decrypt("pass"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String decrypt(String value) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        byte[] bytes = keyStr.getBytes();
        SecretKeySpec key = new SecretKeySpec(Arrays.copyOf(bytes, 16), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key);
        return new String(cipher.doFinal(Base64.decodeBase64(value)),StandardCharsets.UTF_8);
    }

    public static void test02(){
        String key = "abcdefghijklmnop";
        String data = "0123456789";

        String encrypted = encrypt(data, key);
        System.out.println("加密后数据: " +  encrypted);
        String decrypted = decrypt(encrypt(data, key), key);
        System.out.println("解密后数据: "  + decrypted);
    }

    private static void test01(String data) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        // 生成key
//        KeyGenerator kg = KeyGenerator.getInstance("AES");
//        kg.init(128);  // 确定密钥长度
//        byte[] keyBytes = kg.generateKey().getEncoded();
//        System.out.println(new String(keyBytes, StandardCharsets.UTF_8));
        // 格式化key
        byte[] bytes = keyStr.getBytes();
//        Key key = new SecretKeySpec(bytes, "AES");
        Key key = new SecretKeySpec(Arrays.copyOf(bytes,16), "AES");

        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); // 确定算法
//        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); // 确定算法
        cipher.init(Cipher.ENCRYPT_MODE, key);    // 确定密钥，进入加密模式
        byte[] result = cipher.doFinal("0123456789gygyffccddxdx".getBytes());  // 加密
        System.out.println(encodeBase64String(result));  // 不进行Base64编码的话，那么这个字节数组对应的字符串就是乱码

        cipher.init(Cipher.DECRYPT_MODE, key); // 确定密钥，进入解密模式
        System.out.println(new String(cipher.doFinal(result))); // 解密
    }

    public static String encrypt(String input, String key) {
        byte[] crypted = null;
        try {

            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");

//            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, skey);
            crypted = cipher.doFinal(input.getBytes());
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
        }

        //return new String(Base64.encodeBase64(crypted));
        return new String(Hex.encodeHex(crypted)).toUpperCase();
//        return new String(Base64.encodeBase64(crypted));
    }

    public static String decrypt(String input, String key) {
        byte[] output = null;
        try {
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
//            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skey);
//            output = cipher.doFinal(Base64.decodeBase64(input));
            output = cipher.doFinal(Hex.decodeHex(input.toCharArray()));
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return new String(output);
    }
}
